<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of PhpMessageSource
 *
 * @author xuanhoapro
 */
class PhpMessageSource extends CPhpMessageSource {

    private $_messages=array();
    
    /**
     * Auto generate message to language file if it's not existed.
     * @param string $category Language file name
     * @param string $message Message content
     * @param string $language Destination locale
     * @return type
     */
    protected function translateMessage($category, $message, $language) {
        $key = $language . '.' . $category;
        
        if (!isset($this->_messages[$key]))
            $this->_messages[$key] = $this->loadMessages($category, $language);

        if (isset($this->_messages[$key][$message]) && $this->_messages[$key][$message] !== '')
            return $this->_messages[$key][$message];

        $this->_messages[$key][$message] = $message;
        $file = $this->getMessageFile($category, $language);
        
        if (!is_dir(dirname($file))) {
            @mkdir(dirname($file), 0755, true);
        }
        
        @file_put_contents($file, '<?php return ' . var_export((array) $this->_messages[$key], true) . ';');

        if ($this->hasEventHandler('onMissingTranslation')) {
            $event = new CMissingTranslationEvent($this, $category, $message, $language);
            $this->onMissingTranslation($event);
            return $event->message;
        }
        else
            return $message;
    }

}

?>
